	
(function(){
mobileImgScroll=function(){};
mobileImgScroll.prototype={

	init:function(option){
		if(typeof option == "undefined"){
			option = {};
		} 
		var bodyWidth=$(document).width();
		this.Touch = option.Touch || "#J-Touch";	
		this.TouchIco=option.TouchIco || "#J-Touch-ico";
		this.Pagesize=option.Pagesize|| 1;
		this.Width=bodyWidth;	
		this.Current=option.Current|| "on";
		this.isAuto=option.isAuto || false ;		
		this.timer=null;
		this.num=0;		
		if(this.isAuto)
		{
			this.offSet=option.offSet || 3000;
		}
		this._fnStart();
		this._fnIsAuto();	
		this._fnTouch(this.Touch.split("#")[1]);
	},
	_fnLength:function(){
		return $(this.Touch).find("li").length;
	},
	_fnMoveWidth:function(){
		return (this.Pagesize)*(this.Width);
	},
	_fnStart:function(){
		var _this=this,
			$ul=$(_this.Touch),
			$ulIco=$(_this.TouchIco),
			_Current=_this.Current,
			_moveWidth=_this._fnMoveWidth();

		$ul.css({"left":0,width:(this._fnLength())*(this.Width)});
        $(".img_wrap ul li").css("width",this.Width);
		$ul.bind("mouseover touchstart",function(){
			clearTimeout(_this.timer);
			_this.timer=null;
		});	
		$ulIco.bind("mouseover touchstart",function(){
			clearTimeout(_this.timer);
			_this.timer=null;
		});		
		$ul.bind("mouseout touchend",function(){
			_this._fnIsAuto();
		});
		$ulIco.delegate("li","click touchstart",function(){
			var _index=$(this).index();
			_this.num=_index;		
			_this._fnAnimate($ul,-(_index*_moveWidth));
			_this._fnAddClass($ulIco.find("li"),_Current,_index);
		});
	},
	_fnAnimate:function(tag,i){
		tag.stop(true,false).animate({"left":i},150);
	},
	_fnAddClass:function(tag,className,i){
		tag.removeClass(className).eq(i).addClass(className);
	},

	_fnIsAuto:function(){
		var _this=this;
		if(typeof _this.offSet=="undefined")
		{
			return false;
		}
		if(!_this.timer)
		{
			_this.timer=window.setTimeout(function(){_this._fnAuto();},_this.offSet);
		}
	},
	_fnAuto:function(){
		var _this=this;
		_this.num+=1;
		if(_this.num==_this._fnLength())
		{
			_this.num=0;
		}
		if(_this.num<_this._fnLength())
		{
			_this._fnAnimate($(_this.Touch),-(_this.num*(_this._fnMoveWidth())));
			_this._fnAddClass($(_this.TouchIco).find("li"),_this.Current,_this.num);
		}
		clearTimeout(_this.timer);
		_this.timer=window.setTimeout(function(){_this._fnAuto();},_this.offSet);
	},

	_fnTouch:function(id){
		var _this=this,
			move=document.getElementById(id);
			_this._StartX=0;
			_this._StratY=0;
			_this._MoveX=0;
			_this._MoveY=0;
			_this._temp=0;
			
		move.addEventListener("touchstart",function(e){_this._fnTouchStart(e);}, false);
		move.addEventListener("touchmove",function(e){_this._fnTouchMove(e);}, false);
		move.addEventListener("touchend",function(e){_this._fnTouchEnd(e);}, false);
	},
	
	_fnTouchX:function(e){
		var touches = e.changedTouches,
		i = 0, l = touches.length, touch,tagX;
		for (; i < l; i++) {
			touch = touches[i];
			tagX=touch.pageX;
		}
		return tagX;
	},
	_fnTouchY:function(e){
		var touches = e.changedTouches,
		i = 0, l = touches.length, touch,tagY;
		for (; i < l; i++) {
			touch = touches[i];
			tagY=touch.pageY;
		}
		return tagY;
	},
	_fnTouchStart:function(e){
		var _this=this;
		_this._StartX=_this._fnTouchX(e);
		_this._StartY=_this._fnTouchY(e);
		
		_this._temp=$(_this.Touch).position().left;	
	},
	_fnTouchMove:function(e){
		var _this=this;
		_this._MoveX=_this._fnTouchX(e)-_this._StartX;
		_this._MoveY=_this._fnTouchY(e)-_this._StartY;
		
		
		if(Math.abs(_this._MoveY)<Math.abs(_this._MoveX))
		{
			e.preventDefault();	
			var moveX=_this._temp+_this._MoveX;
			$(_this.Touch).css({"left":moveX+"px"});
		}		
	},
	_fnTouchEnd:function(e){
		var _this=this;
		
		_this._MoveX=_this._fnTouchX(e)-_this._StartX;
		_this._MoveY=_this._fnTouchY(e)-_this._StartY;
		if(Math.abs(_this._MoveY)<Math.abs(_this._MoveX))
		{
			e.preventDefault();
			
			if(_this._MoveX>0)
			{	
				_this.num--;
				if(_this.num>=0)
				{
					var moveX=(_this.num)*(_this._fnMoveWidth());
					_this._fnAnimate($(_this.Touch),-moveX);
					_this._fnAddClass($(_this.TouchIco).find("li"),_this.Current,_this.num);
				}
				else
				{
					
					this._fnAnimate($(_this.Touch),0);
					_this.num=0;
				}
			}else{
			
				_this.num++;
				if(_this.num<_this._fnLength() && _this.num>=0)
				{
					var moveX=(_this.num)*(_this._fnMoveWidth());
					_this._fnAnimate($(_this.Touch),-moveX);
					_this._fnAddClass($(_this.TouchIco).find("li"),_this.Current,_this.num);
				}
				else
				{
					
					_this.num=_this._fnLength()-1;
					this._fnAnimate($(_this.Touch),-(_this.num*(_this._fnMoveWidth())));
				}
			}
		}		
	}
};	
})(window);